
local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26							//number of times windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort window size						

global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

local controls_fes="science_cites originality i.wipo_cited_broad"

//=======================================
// TABLE A5
//=======================================


use "$DATA\twincites_applicant_did_prep_prenorm.dta", clear

drop if backcite_count_citing>100
sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force
duplicates tag family_cited, gen(tag)
drop if tag==0
drop tag
replace lag=exp(lag)+546
joinby family_cited using "$DATA\filing_dates_diffs.dta"
rename assignee_cited assignee
drop unique_inventors_citing_2000
merge m:1 assignee using "$DATA\assignee_unique_inventors_2000_all.dta", nogen keep(match master)
rename assignee assignee_cited
rename unique_inventors_citing_2000 rd_scale_cited

	replace cited_jp_it_fc=ln(cited_jp_it_fc+1)
	quietly: sum cited_jp_it_fc
	replace cited_jp_it_fc=(cited_jp_it_fc-r(mean))/r(sd)
	
	replace science_cites=ln(science_cites+1)
	quietly: sum science_cites
	replace science_cites=(science_cites-r(mean))/r(sd)
	
	quietly: sum originality
	replace originality=(originality-r(mean))/r(sd)
	
	replace inventors_citing=ln(inventors_citing)
	quietly: sum inventors_citing
	replace inventors_citing=(inventors_citing-r(mean))/r(sd)


replace rd_scale_cited=0 if rd_scale_cited==.
gen log_rd_scale=log(rd_scale_cited+1)
quietly: sum log_rd_scale
replace log_rd_scale=(log_rd_scale-r(mean))/r(sd)

reg diff i.after_change_cited log_rd_scale `controls_fes', vce(robust)

foreach p in 14 28 {
	gen early_`p'=abs(diff)<(365-`p')
	logistic early_`p' i.after_change_cited log_rd_scale `controls_fes', vce(robust)
}

//=======================================
// TABLE A6
//=======================================


use "$DATA\twincites_applicant_did_prep_prenorm.dta", clear

drop if backcite_count_citing>100
sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force
duplicates tag family_cited, gen(tag)
drop if tag==0
drop tag
replace lag=exp(lag)+546
joinby family_cited using "$DATA\filing_dates_diffs.dta"
rename assignee_cited assignee
drop unique_inventors_citing_2000
merge m:1 assignee using "$DATA\assignee_unique_inventors_2000_all.dta", nogen keep(match master)
rename assignee assignee_cited
rename unique_inventors_citing_2000 rd_scale_cited

	replace cited_jp_it_fc=ln(cited_jp_it_fc+1)
	quietly: sum cited_jp_it_fc
	replace cited_jp_it_fc=(cited_jp_it_fc-r(mean))/r(sd)
	
	replace science_cites=ln(science_cites+1)
	quietly: sum science_cites
	replace science_cites=(science_cites-r(mean))/r(sd)
	
	quietly: sum originality
	replace originality=(originality-r(mean))/r(sd)
	
	replace inventors_citing=ln(inventors_citing)
	quietly: sum inventors_citing
	replace inventors_citing=(inventors_citing-r(mean))/r(sd)


replace rd_scale_cited=0 if rd_scale_cited==.
gen log_rd_scale=log(rd_scale_cited+1)
quietly: sum log_rd_scale
replace log_rd_scale=(log_rd_scale-r(mean))/r(sd)

reg diff i.after_change_cited##c.cited_jp_it_fc  log_rd_scale `controls_fes', vce(robust)

foreach p in 14 28 {
	gen early_`p'=abs(diff)<(365-`p')
	logistic early_`p' i.after_change_cited##c.cited_jp_it_fc log_rd_scale `controls_fes', vce(robust)
}


